package com.y.mytest.utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import okhttp3.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: testHttp
 * @author: 叶子豪
 * @create: 2021-11-13 15:55
 */
public class OkHttpUtil {
    public static void sendHttpRequest(String url, Map<Object, Object> allData) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("allData", JSON.toJSONString(allData, SerializerFeature.WriteMapNullValue))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .method(HttpUtil.GET, body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            JSONObject data = (JSONObject) jsonObject.get("data");
            List<Map<String, String>> list = (List<Map<String, String>>) data.get("list");
            System.out.println(list);
            for (Map<String, String> stringIntegerMap : list) {
                String date = String.valueOf(stringIntegerMap.get("date"));
                System.out.println("( 0, 0, '" + date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6) + "', NULL, 1 ),");
            }
            /*list.forEach((k) -> {
                System.out.println("( 0, 0, '" + k.get("date") + "', NULL, 1 ),");
            });*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
